﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Studio;
using Telerik.Windows;
using Zeus.Classes.Engines.Scripting_Engine;
using Zeus.Classes.Events;
using Zeus.Events;
using Zeus.Classes.Documents;
using Zeus.Classes.Engines.Image_Engine;
using Zeus.Classes.Engines.Sound_Engine;
using Zeus.Classes.Engines.Video_Engine;
using Zeus.Classes.Views;


namespace Zeus.UserControls
{
    public partial class TabContral : UserControl
    {
        ObservableCollection<MyViewModel> myViewModelCollection = new ObservableCollection<MyViewModel>();
       
        public TabContral()
        {
            InitializeComponent();
            tabControl.ItemsSource = myViewModelCollection;
            //Handle the clicked event:
            tabControl.AddHandler(EventHelper.ClickEvent, new RoutedEventHandler(OnCloseClicked));
           
            App.EventAggegator.GetEvent<AddNewTab>().Subscribe(AddNewTab);
            App.EventAggegator.GetEvent<CloseSolution>().Subscribe(CloseAll);
            App.EventAggegator.GetEvent<PhysicsTab>().Subscribe(AddPhysicsTab);
        }
        public void OnCloseClicked(object sender, RoutedEventArgs e)
        {
            var radEventArgs = e as RadRoutedEventArgs;
            var button = radEventArgs.Source as Button;
            var model = button.DataContext as MyViewModel;
            if (model != null)
            {
                myViewModelCollection.Remove(model);
            }
        }
        private void AddPhysicsTab(object o)
        {
            myViewModelCollection.Add(new MyViewModel
            {
                Title = "Physics Sample",
                View = null,
                Content = new FarseerGames.SimpleSamplesSilverlight.Page()
            });
        }
        private void AddNewTab(View view)
        {
            Document doc = view.CDocument;
            switch (doc.DocType)
            {
                case ProjectItem_Types.Class:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new EditorText(doc)
                        });
                        break;
                    }
                case ProjectItem_Types.Image:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new EditorImage(doc)
                        });
                        break;
                    }
                case ProjectItem_Types.Sound:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new EditorSound(doc)
                        });
                        break;
                    }
                case ProjectItem_Types.Video:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new EditorVideo(doc)
                        });
                        break;
                    }
                case ProjectItem_Types.Scene:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new Scene2DUserControl()
                        });
                        break;
                    }
                case ProjectItem_Types.Text:
                    {
                        myViewModelCollection.Add(new MyViewModel
                        {
                            Title = doc.FileName,
                            View = view,
                            Content = new EditorText(doc)
                        });
                        break;
                    }
            }


        }
        private void CloseAll(Object obj)
        {
            myViewModelCollection.Clear();
        }
    }
    public class MyViewModel
    {
        public String Title { get; set; }
        public View View { get; set;}
        public object Content { get; set; }
    }

}
